﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class UserQuickLinkProcessBll 
    {
        public static UserQuickLinkCls[] ReadingUserQuickLink(string SiteId, string UserId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableUserQuickLink where frkUserId='" + UserId + "' order by EntryDate DESC",
                    null);
            if (dsResult.Tables[0].Rows.Count ==  0)
            {
                return new UserQuickLinkCls[0];
            }
            UserQuickLinkCls[] CmsUserQuickLinks = UserQuickLinkParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsUserQuickLinks;
        }

        public static UserQuickLinkCls CreateUserQuickLinkModel(string SiteId, string UserQuickLinkId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableUserQuickLink " +
                                               "     where UserQuickLinkId=@UserQuickLinkId",
                    new DbParam[] 
                    { 
                        new DbParam("UserQuickLinkId", UserQuickLinkId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            UserQuickLinkCls OUserQuickLink = UserQuickLinkParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OUserQuickLink;
        }



        public static void AddUserQuickLink(
           string SiteId,
           UserQuickLinkCls OUserQuickLink)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string UserQuickLinkId = System.Guid.NewGuid().ToString();
            DBService.Insert(null, "TableUserQuickLink",
                    new DbParam[] 
                    { 
                        new DbParam("UserQuickLinkId", UserQuickLinkId),
                        new DbParam("frkUserId", WebSessionUtility.GetCurrentLoginUser().UserId),
                        new DbParam("Subject",OUserQuickLink.Subject),
                        new DbParam("LinkUrl",OUserQuickLink.LinkUrl),
                        new DbParam("EntryDate",System.DateTime.Now),
                        new DbParam("SortIndex",OUserQuickLink.SortIndex),
                        new DbParam("LinkType",OUserQuickLink.LinkType),
                        new DbParam("ObjectId",OUserQuickLink.ObjectId),
                    });
        }


        public static void DeleteUserQuickLink(string SiteId, string UserQuickLinkId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableUserQuickLink where UserQuickLinkId=@UserQuickLinkId ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("UserQuickLinkId", UserQuickLinkId),
                    });
        }
    }
}

